< previous page page_57 next page >

Page 57
Type.gifListing 3.9. Coding the cmdSaveBookmarks_Click event for BOOKMARKS.VBP.
Private Sub cmdSaveBookmark_Click()
    '
    ' show how bookmarks work
    '
    Static blnFlag As Boolean
    Static strBookmark As String
    '
    If blnFlag = False Then
        '
        ' flip flag and set caption
        blnFlag = True
        cmdSaveBookmark.Caption = "&Restore Bookmark"
        '
        ' save bookmark for later
        strBookmark = dtaBookMarks.Recordset.Bookmark
        MsgBox "Bookmark Saved", vbInformation
    Else
        '
        ' flip flag and set caption
        blnFlag = False
        cmdSaveBookmark.Caption = "&Save Bookmark"
        '
        ' restore saved bookmark
        dtaBookMarks.Recordset.Bookmark = strBookmark
    End If
    '
End Sub
Tip.gif
Listing 3.9 uses two Static variables. Static variables keep their value even after the procedure ends. Using Static variables in your program is an excellent way to keep track of flag values even after procedures or functions exit. The only other way to make sure that variables maintain their value after exit from a routine is to place them in the declaration area of the form. The problem with placing them at the form-level declaration is that they now can be altered by routines in other procedures or functions on the same form. Declaring Static variables within the procedures in which they are used follows good programming practice by limiting the scope of the variable.

Save the form as BOOKMARKS.FRM and the project as BOOKMARKS.VBP, and then run the program. The program opens the BOOKS5.MDB file, creates a Dynaset-type Recordset of all the records in the Authors data table, and presents the first record on the form. Note that the command

 
< previous page page_57 next page >

If you like this book, buy it!